package com.kltn.zigo.data.loader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;
import android.widget.ListView;

import com.kltn.zigo.data.IP;
import com.kltn.zigo.process.JSONParser;

public class PlaceDetailLoader {
	JSONParser jParser = new JSONParser(); 
    
	ListView list;
	
    //private static String url_all_place = IP.get_IP() +  "/zigo/KLTN/WebservicePHP/get_all_place.php";
	private static String url_place_detail = IP.get_IP() +  IP.get_path_service() + "get_place_detail.php";
 // JSON Node names
    public final String TAG_SUCCESS = "success";
    public final String TAG_PLACE = "place";
    public final String TAG_PLACE_ID = "place_id";
    public final String TAG_PLACE_NAME = "place_name";
    public final String TAG_ADDRESS = "address";
    public final String TAG_DISTRICT_ID = "district_id";
    public final String TAG_DISTRICT_NAME = "district_name";
    public final String TAG_PROVINCE_NAME = "province_name";
    public final String TAG_LONGITUDE = "longitude";
    public final String TAG_LATITUDE = "latitude";
    public final String TAG_PHONE_NUMBER = "phone_number";
    public final String TAG_FAX_NUMBER = "fax_number";
    public final String TAG_EMAIL = "email";
    public final String TAG_WEBSITE = "website";
    public final String TAG_OPEN_TIME = "open_time";
    public final String TAG_CLOSE_TIME = "close_time";
    public final String TAG_LOWEST_PRICE = "lowest_price";
    public final String TAG_HIGHTEST_PRICE = "hightest_price";
    public final String TAG_UTILITY = "utility_name";
    public final String TAG_SPACE_PLACE = "space_place_name";
    public final String TAG_GROUP_CUSTOMER = "group_customer_name";
    public final String TAG_DESCRIPTION = "description";
    public final String TAG_URL_AVATAR = "url_avatar";
    public final String TAG_NAME_AVATAR = "name_avatar";

	String place_id;
	
    // products JSONArray
    JSONArray places = null;
    
    public PlaceDetailLoader(String PlaceID)
    {
    	place_id = PlaceID;
    }
    
    public HashMap<String, String> GetPlace()
    {
    	HashMap<String, String> placesList = new HashMap<String,String>();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("place_id", place_id));
		
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_place_detail, "GET", params);

        Log.d("Places detail: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                places = json.getJSONArray(TAG_PLACE);

                // looping through All Products
                for (int i = 0; i < places.length(); i++) {
                    JSONObject c = places.getJSONObject(i);

                    // Storing each json item in variable
                    String placeName = c.getString(TAG_PLACE_NAME);
                    String address = c.getString(TAG_ADDRESS);
                    String provincename = c.getString(TAG_PROVINCE_NAME);
                    String districtname = c.getString(TAG_DISTRICT_NAME);
                    String longitude = c.getString(TAG_LONGITUDE);
                    String latitude = c.getString(TAG_LATITUDE);
                    String phonenumber = c.getString(TAG_PHONE_NUMBER);
                    String faxnumber = c.getString(TAG_FAX_NUMBER);
                    String email = c.getString(TAG_EMAIL);
                    String website = c.getString(TAG_WEBSITE);
                    String opentime = c.getString(TAG_OPEN_TIME);
                    String closetime = c.getString(TAG_CLOSE_TIME);
                    String lowestprice = c.getString(TAG_LOWEST_PRICE);
                    String hightestprice = c.getString(TAG_HIGHTEST_PRICE);
                    String utility = c.getString(TAG_UTILITY);
                    String spaceplace = c.getString(TAG_SPACE_PLACE);
                    String groupcustomer = c.getString(TAG_GROUP_CUSTOMER);
                    String description = c.getString(TAG_DESCRIPTION);
                    String urlavatar = c.getString(TAG_URL_AVATAR);
                    String nameavatar = c.getString(TAG_NAME_AVATAR);
                    String districtid = c.getString(TAG_DISTRICT_ID);

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ADDRESS, address);
                    map.put(TAG_PLACE_NAME, placeName);
                    map.put(TAG_CLOSE_TIME, closetime);
                    map.put(TAG_DESCRIPTION, description);
                    map.put(TAG_DISTRICT_NAME, districtname);
                    map.put(TAG_EMAIL, email);
                    map.put(TAG_FAX_NUMBER, faxnumber);
                    map.put(TAG_HIGHTEST_PRICE, hightestprice);
                    map.put(TAG_LATITUDE, latitude);
                    map.put(TAG_LONGITUDE, longitude);
                    map.put(TAG_LOWEST_PRICE, lowestprice);
                    map.put(TAG_PHONE_NUMBER, phonenumber);
                    map.put(TAG_OPEN_TIME, opentime);
                    map.put(TAG_PROVINCE_NAME, provincename);
                    map.put(TAG_WEBSITE, website);
                    map.put(TAG_URL_AVATAR, urlavatar);
                    map.put(TAG_NAME_AVATAR, nameavatar);
                    map.put(TAG_UTILITY, utility);
                    map.put(TAG_SPACE_PLACE, spaceplace);
                    map.put(TAG_GROUP_CUSTOMER, groupcustomer);
                    map.put(TAG_DISTRICT_ID, districtid);

                    // adding HashList to ArrayList
                    //placesList.add(map);
                    placesList = map;
                }
            } 
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return placesList;
    }
}
